package com.eliza.kotlin.design.creation.single.demo9

import com.eliza.ktor.all.library.utils.ToolsLog
import java.io.InputStream
import java.net.InetAddress
import java.nio.charset.Charset
import java.util.*
import kotlin.reflect.full.*


/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 6/4/22
 * @Software: IntelliJ IDEA
 */
fun RunCommand(command: String) {
    val runtime = Runtime.getRuntime()


    //返回 Java 虚拟机中的内存总量。
    ToolsLog.LogPrintln(runtime.totalMemory());
    //返回 Java 虚拟机试图使用的最大内存量。
    ToolsLog.LogPrintln(runtime.maxMemory());


    //调用runtime的方法exec, 参数要的是一个命令
    /**
     * 对应 操作系统 终端命令
     * */
    val process: Process = runtime.exec(command)
    //调用process对象的获取输入流的方法 取命令执行后的结果，通过输入流获取
    val inputStream: InputStream = process.inputStream
    val arr = ByteArray(1024 * 1024 * 100)

    //读取数据
    val len = inputStream.read(arr) //返回读到的字节的个数

    //将字节数组转换为字符串输出到控制台
//    println(String(arr, 0, len, Charset.defaultCharset()))
    ToolsLog.LogPrintln(String(arr, 0, len, Charset.forName("GBK")), 29)

}


fun getSystemInfo2() {
    val inetAddressKClass = InetAddress.getLocalHost()::class

    val memberProperties = inetAddressKClass.memberProperties//获取KClass对象的public属性
    val declaredMembers = inetAddressKClass.declaredMembers
    val declaredFunctions = inetAddressKClass.declaredFunctions
    val declaredMemberFunctions = inetAddressKClass.declaredMemberFunctions
    val declaredMemberProperties = inetAddressKClass.declaredMemberProperties//获取KClass对象的所有属性
    for (i in memberProperties) {
        if (i.name.indexOf("get") >= 0) {
            ToolsLog.LogPrintln(i.name)

            i.call()
        }

    }


}

fun getSystemInfo(): String {
    val localHost = InetAddress.getLocalHost()
    ToolsLog.LogPrintln(localHost.hostName)
    ToolsLog.LogPrintln(localHost.hostAddress)
    val toLowerCase = System.getProperty("os.name").lowercase(Locale.getDefault())
    ToolsLog.LogPrintln(toLowerCase, 4)
    return toLowerCase
}

fun main(args: Array<String>) {
    val systemInfo = getSystemInfo()
    if (systemInfo.contains("windows")) {
        RunCommand("ipconfig")
    } else if (systemInfo.contains("linux")) {
        RunCommand("ip addr")
    }

}